import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/17 15:51
 * @description hashMap + 求余
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int minimumRounds(int[] tasks) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int v: tasks) {
            map.put(v, map.getOrDefault(v, 0) + 1);
        }
        int ans = 0;
        for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
            int num = entry.getValue();
            int temp = 0;
            for (int i = num / 3; i >= 0; i--) {
                // 优先分配 3，如果剩下的能分2，则直接跳出
                int left = num - i * 3;
                if (left % 2 == 0) {
                    temp += i + left / 2;
                    break;
                }
            }
            if (temp == 0) {
                return -1;
            }
            ans += temp;
        }
        return ans;
    }
}
